package chalmers.game.subterranean.ui.view;

import chalmers.game.subterranean.event.Bus;
import chalmers.game.subterranean.event.events.RestartGameEvent;
import chalmers.game.subterranean.event.events.player.JumpEvent;
import chalmers.game.subterranean.event.events.player.MoveOnGroundEvent;
import chalmers.game.subterranean.event.events.world.DestroyBlockEvent;
import chalmers.game.subterranean.util.options.Sound;
import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import javafx.scene.media.AudioClip;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import org.apache.log4j.Logger;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * User: Mikael Friederici Date: 2014-05-02 Time: 16:51
 */
public class SoundHandler {

  private MediaPlayer musicPlayer;
  private static Logger logger = Logger.getLogger("subLog");
  private AudioClip effectsCH1;
  private AudioClip effectsCH2;
  ExecutorService soundPool = Executors.newFixedThreadPool(3);

  public SoundHandler() {
    Bus.INSTANCE.register(this);
    // playMusic(Variables.LEVEL1_SOUND);
    effectsCH1 = Sound.MOVE_SOUND;
    effectsCH2 = Sound.MOVE_SOUND;
    // musicPlayer = new MediaPlayer(new Media(Variables.MOVE_SOUND));
  }

  @Subscribe
  @AllowConcurrentEvents
  public void jump(JumpEvent event) {
    playSoundCH2(Sound.JUMP_SOUND);
  }

  @Subscribe
  @AllowConcurrentEvents
  public void move(MoveOnGroundEvent event) {
    playSoundCH1(Sound.MOVE_SOUND);
  }

  /*
   * @Subscribe @AllowConcurrentEvents public void dig(DamageBlockEvent event){
   * playSoundCH1(Variables.DIG_SOUND); }
   */
  @Subscribe
  @AllowConcurrentEvents
  public void destroyBlock(DestroyBlockEvent event) {
    playSoundCH1(Sound.DESTROY_BLOCK_SOUND);
  }

    public void playSoundCH1(AudioClip sound) {
    if (!effectsCH1.isPlaying()) {
      Runnable soundPlay = new Runnable() {
        @Override
        public void run() {
            effectsCH1 = sound;
          logger.info("Playing audio effects CH1");
          effectsCH1.play(Sound.EFFECTS_VOLUME);
        }
      };
      soundPool.execute(soundPlay);
    }
  }

  public void playSoundCH2(AudioClip sound) {
    Runnable soundPlay = new Runnable() {
      @Override
      public void run() {
        effectsCH2 = sound;
        logger.info("Playing audio effects CH2");
        effectsCH2.play(Sound.EFFECTS_VOLUME);
      }
    };
    soundPool.execute(soundPlay);
  }

  public void playMusic(String musicURL) {
    // URL resource = getClass().getResource(musicURL);
    // Media media = new Media(resource.toString());
    Runnable soundPlay = new Runnable() {
      @Override
      public void run() {
        Media media = new Media(musicURL);
        musicPlayer = new MediaPlayer(media);
        logger.info("Playing background music " + musicURL);
        musicPlayer.setCycleCount(AudioClip.INDEFINITE);;
        musicPlayer.setVolume(Sound.MUSIC_VOLUME);
        musicPlayer.play();
      }
    };
    soundPool.execute(soundPlay);
  }

  public void pauseMusic() {
    musicPlayer.pause();
  }

  public void resumeMusic() {
    musicPlayer.play();
  }

  public void stopMusic() {
    musicPlayer.stop();
  }

  @Subscribe
  public void handleRestartGame(RestartGameEvent evt) {
    Bus.INSTANCE.unRegister(this);
  }
}
